package com.dragon.sdgp.config;

import com.dragon.sdgp.common.security.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    @Autowired
//    private LoginSuccessHandler loginSuccessHandler;
//
//    @Autowired
//    private LoginFailureHandler loginFailureHandler;

    @Autowired
    private MyUserDetailsServiceImpl myUserDetailsService;

    @Autowired
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Autowired
    private JwtAccessDeniedHandler jwtAccessDeniedHandler;

    /**
     * 白名单
     */
    public static final String[] URL_WHITELIST = {
            "/api/common/vCode",
            "/api/SysUser/register",
            "/api/SysUser/login",
            "/api/common/create",
            "/api/lexicon/list",
            "/api/lexicon/lexiconItem",
    };

    /**
     * 配置密码加密方式
     * @return
     */
    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        return new JwtAuthenticationFilter(authenticationManager());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //csrf攻击关闭
        http.csrf()
                .disable();

        //解决跨域
        http.cors();

        //登录登出配置
        http.formLogin().disable();
//        http.formLogin()
//                .loginProcessingUrl("/login")
//                .successHandler(loginSuccessHandler)
//                .failureHandler(loginFailureHandler);

        //异常处理
        http.exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .accessDeniedHandler(jwtAccessDeniedHandler);

        //session禁用配置
        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);//无状态
        //拦截规则配置
        http.authorizeRequests()
                .antMatchers(URL_WHITELIST) //白名单放行
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
//                .addFilterBefore(verificationCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilter(jwtAuthenticationFilter());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService);
    }
}
